<?php

namespace App\Listeners;

use App\Events\RepayMentEvent;
use App\Facades\CollectionRequestFacade;
use App\Library\ArrayTools\ArrayTools;
use App\Models\Collection\RepayRecord;
use App\Models\RepaySummary as RepaySummaryModel;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Constant\QueueKey;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class RepayMentEventListener implements ShouldQueue
{

    use InteractsWithQueue;

    public $queue = QueueKey::QUEUE_COLLECTION_QUEUE_ONE;

    public $timeout = 600; //秒数

    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  RepayMentEvent  $event
     * @return void
     */
    public function handle(RepayMentEvent $event)
    {
        $summary_id = $event->summary_id;
        $summaries = RepaySummaryModel::leftJoin('repay_records as a', 'repay_summaries.id', '=', 'a.summary_id')
            ->leftJoin('repay_record_months as b', 'a.id', '=', 'b.record_id')
            ->leftJoin('repay_record_moneys as c', 'a.id', '=', 'c.record_id')
            ->leftJoin('repay_record_commons as d', 'a.id', '=', 'd.record_id')
            ->where('repay_summaries.id', $summary_id)
            ->where('a.period', '>', 0)
            ->select([
                'repay_summaries.id as summary_id',
                'repay_summaries.loan_id as case_code',
                'repay_summaries.uid',
                'repay_summaries.period',
                'repay_summaries.period_count',
                'repay_summaries.amount',
                'repay_summaries.rate',
                'repay_summaries.data_type',
                'repay_summaries.trans_id',
                'repay_summaries.loan_id',
                //a 是repay_records
                'a.id as record_id',
                'a.amount as record_amount',
                'a.period as record_period',
                'a.status as record_status',
                'a.normal_date',
                'a.pay_off as pay_off',
                'a.repay_plan_number as repay_plan_number',
                'a.created_at as record_create_at',
                'a.repay_date as repay_date',
                'a.pay_off as repay_pay_off',
                //b 是repay_record_months
                'b.amount as month_amount',
                'b.status as month_status',
                //c 是repay_record_moneys
                'c.fines_money as money_fines',
                'c.lending_money as money_lending',
                'c.manage_money as money_manage',
                'c.status as money_status',
                //d 是repay_record_commons
                'd.fines_money as common_fines',
                'd.lending_money as common_lending', //兼容老数据
                'd.manage_money as common_manage',
                'd.relief_amount as common_relief_amount',
                'd.status as common_status',
            ])
            ->orderBy('a.period', 'asc')
            ->get();
        $summaries_exchange = ArrayTools::valuesToKey($summaries->toArray(), 'summary_id');
        //单个还款订单为单位
        foreach ($summaries_exchange as $key => $records) {
            if (empty($records))
                continue;
            try {
                $collection_info = RepayRecord::getCollectionInfo($records, true);
                $case_msg = $collection_info['caseMsg'];
                $repay_planList = $collection_info['repayPlanList'];
                $repay_record = $collection_info['repayRecord'];
                $result = CollectionRequestFacade::sendRepayMentCase($case_msg, $repay_record, $repay_planList);
                Log::debug('send_repayment_case_result', ['result' => $result, 'summary_id' => $summary_id]);
            } catch (\Exception $e) {
                Log::debug('send_repayment_case_exception', [
                    'summary_id' => $summary_id,
                    'file' => $e->getFile(),
                    'line' => $e->getLine(),
                    'message' => $e->getMessage(),
                ]);
            }
        }
    }
}
